import { Component,ElementRef, OnInit,Input,ViewChild, Output, EventEmitter,AfterViewInit} from '@angular/core';
import { TreeModule,TreeNode} from 'primeng/primeng';
import { HttpClientModule,HttpClient,HttpErrorResponse} from '@angular/common/http';

import { EffectService }from '../../user/services/effect.service';
import { Service } from '../../server/server';
import { EffectTableComponent } from '../effect/effect-table/effect-table.component';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import { Pagination } from '../../public/serve/pcrudInfo';
import { defaultSearchType } from '../../public/serve/pcrudInfo';
import { paginationData } from './effect-data';
import { HintsService } from '../../public/serve/hints/hints.service';

declare var $:any;

@Component({
  selector: 'app-effect',
  templateUrl: './effect.component.html',
})
export class EffectComponent implements OnInit {
  
  @ViewChild("table") table: EffectTableComponent;

  @Output() treeOutput: EventEmitter<any> = new EventEmitter();

  treeDatas: TreeNode[];
  selectedFiles: any[];  

  tableNumber : number = 0;
  
  right: any = { "user" : {}, "effect" : {} };
  constructor(
    private service: EffectService,
    private server :Service,
    private removeTableService: RemoveTableService,
    private tableListService:CudTableService,
    private http:HttpClient,
    private cudTableService: CudTableService,
    private hintsService:HintsService
  ) { 
    this.right = $.extend(this.right, server.getRight());
  }
  
  isActive= this.server.isActive;
  ngOnInit() {
    this.getParentInfos();
    this.server.eventEmit.subscribe((value: any) => { this.isActive=value; });
  }

  ngDoCheck(){
    this.right = $.extend(this.right, this.server.getRight());
  }
  //树形菜单data
  getParentInfos(): void {
    this.service.getParentInfos(null).then((data: any) => {
      this.treeDatas = data;
      this.treeDatas.forEach( node => {
        if(this.superioraddId == node.data ){
         this.expandRecursive(node, true);
        }
     });
      this.treeOutput.emit(data.result_code);
    }).catch(err => {
        console.error('请检查您的网络！');
    });
  }
  //点击选中数据
  superior : string;
  superioraddId : string;
  nodeSelect(event) {

    paginationData.id = event.node.functionId;
    this.table.getTabList(paginationData);
    this.treeTopActive = false;
    if(event.node.parentId){
      this.superior = event.node.functionName;
      this.superioraddId = event.node.functionId;
    }

  }

  expandRecursive(node:TreeNode, isExpand:boolean) {
    node.expanded = isExpand;
    if(node.children){
        node.children.forEach( childNode => {
            this.expandRecursive(childNode, isExpand);
        } );
    }
  }

  // 表格
  totalItems: number;
  ParentList(totalItems: number) {
    this.totalItems = totalItems;
  }

  // 删除按钮
  isSelect: boolean = true;
  isSelectFromTableHandler(isSelect: boolean) {
    this.isSelect = isSelect;
  }

  // 禁用
  reduction() {
    setTimeout(() => {
      this.isSelect = true;
    }, 100);
  }

  //详情
  tabVal: object;
  ParentDetails(tabVal: object) {
    this.tabVal = tabVal;
  }

   //新增
   ParentAdd(result_code: string) {
     this.table.getTabList(paginationData);
     this.getParentInfos();
   }

  // 修改
  ParentUpdate(result_code: string) {
    this.table.getTabList(paginationData);
  }

  /**
   * 权限
   * @param result_code 
   */
  ParentPermission(result_code: string){ 
    this.table.getTabList(paginationData);
  }

  //删除
  ParentDelete(result_code: string) {
    this.table.getTabList(paginationData);
    this.getParentInfos();
    this.table.cancelSelectAll();
  }

  /**
   * 禁用
   */ 
  _disable(status) {
    this.disableEnable(status);
  }

  /**
   * 启用
   */
  _enable(status) {
    this.disableEnable(status);
  }

  /**
   * 分页
   * @param pagination 
   */  
   isLoadTableFromPaginationHandler(pagination: Pagination) {
     paginationData.pageNumber = pagination[0].pageNumber;
     paginationData.pageSize = pagination[0].pageSize;

     this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

     this.table.getTabList(paginationData);
   }

   allPermission : Array<string>;
   allPermissionHandler(allPermission: Array<any>) {
    this.allPermission = allPermission;
   }
  
  /**
   * 检索
   * @param info 
   */  
  retrieval(info) {
    paginationData.queryCriteria = info;
    this.table.getTabList(paginationData);
  }  

  /**
   * 清空检索
   */ 
  emptyRetrieval() {
    paginationData.queryCriteria = '';
  }

  /**
   * 最上级
   * @param id
   */ 
  public treeTopActive:boolean = true;
  toMogami(id) {
    paginationData.id = id;
    this.superioraddId = '0';
    this.table.getTabList(paginationData);
    //console.log(this.selectedFiles)
    this.selectedFiles = []
    this.treeTopActive = true;
  }

  /**
   * 禁用/启用
   */ 
  disableEnable(status: number) {

    let url = this.server.baseUrl+'user/functions/modifyStatus';

    this.removeTableService
        .confirmRemove('functionId', url, 'enabled', status)
        .then((res: any)=> {
          this.hintsService.messages(res.message,res.result_code);
          switch (res.result_code) {
            case '0000':
              this.operationHints(res.message, 'success');
              this.table.getTabList(paginationData);
              this.table.isChose = false;
              this.table.isSelectAll = false;
              this.isSelect = true;
              break;
            default:
              this.operationHints(res.message, 'fail');
              break;
          }
        })
        .catch((err: any)=> {
          console.error('请检查您的网络！');
          return false;
        });
  }

  /**
  * 操作提示
  * @param whichHints
  */
  operationHints(hintsTxt: string, whichHints: string) {
    switch (whichHints) {
      case 'success':
        this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: hintsTxt };
        this.server.promptclose;
        break;
      case 'fail':
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: hintsTxt };
        this.server.promptclose = [];
        break;
    }
  }

}
